package com.ysy.logistics.service.system.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ysy.common.resp.ResultData;
import com.ysy.logistics.entities.system.Dict;
import com.ysy.logistics.entities.system.dto.DictDto;
import com.ysy.logistics.mapper.system.DictMapper;
import com.ysy.logistics.service.system.DictService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassDescription:
 * @Author:姚斯羽
 * @Create:2025/3/19 10:16
 **/
@Service
public class DictServiceImpl extends ServiceImpl<DictMapper, Dict> implements DictService {
    @Resource
    private DictMapper dictMapper;

    private List<DictDto> addChildren(List<Dict> list) {
        if (CollUtil.isEmpty(list)){
            return null;
        }
        return list.stream().map(dict -> {
            LambdaQueryWrapper<Dict> lqw = new LambdaQueryWrapper<>();
            lqw.select().eq(Dict::getPid, dict.getId());
            return dict.toDto(this.list(lqw));
        }).collect(Collectors.toList());
   }

    @Override
    public List<DictDto> findByNameLike(String name) {
        LambdaQueryWrapper<Dict> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.select().like(Dict::getName, name);
        List<Dict> list = this.list(lambdaQueryWrapper);
        if (list.isEmpty()){
            return null;
        }
        //查询子节点
        return addChildren(list);
    }

    @Override
    public List<DictDto> findByPid(long pid) {
            LambdaQueryWrapper<Dict> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.select().eq(Dict::getPid, pid);
            List<Dict> list = this.list(lambdaQueryWrapper);
            if (list.isEmpty()){
                return null;
            }
            //查询子节点
            return addChildren(list);
    }

    @Override
    public ResultData addDict(DictDto dict) {
        //判断有没有该字典
        LambdaQueryWrapper<Dict> lqw = new LambdaQueryWrapper<>();
        lqw.select().eq(Dict::getName, dict.getName())
                .eq(Dict::getPid, 0);
        List<Dict> list = this.list(lqw);
        if(CollUtil.isNotEmpty(list)){
            return ResultData.fail("该字典已存在");
        }
        //保存父节点
        Dict pd = new Dict();
        pd.setName(dict.getName());
        this.save(pd);
        //保存子节点
        List<Dict> children = dict.getChildren();
        if (CollUtil.isNotEmpty(children)){
            children.forEach(child -> {
                child.setPid(pd.getId());
            });
            this.saveBatch(children);
        }
return  ResultData.success();
    }

    @Override
    public ResultData update(DictDto dict) {
        //修改父节点
        Dict pd = new Dict();
        pd.setId(dict.getId());
        pd.setName(dict.getName());
        this.updateById(pd);
        //删除子节点
        LambdaQueryWrapper<Dict> lqw = new LambdaQueryWrapper<>();
        lqw.select(Dict::getId).eq(Dict::getPid, dict.getId());
        this.remove(lqw);
        //保存子节点
        List<Dict> children = dict.getChildren();
        if (CollUtil.isNotEmpty(children)){
            children.forEach(child -> {
                child.setPid(pd.getId());
            });
            this.saveBatch(children);
        }
        return ResultData.success();
    }

    @Override
    public List<DictDto> getDictsByPname(String dictName) {
        LambdaQueryWrapper<Dict> lqw = new LambdaQueryWrapper<>();
        lqw.select().eq(Dict::getName, dictName);
            List<Dict> list = this.list(lqw);
            return addChildren(list);
    }
}
